﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Diagnostic : System.Web.UI.Page
{
    ArticleItem article;
    int iCatID;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetCategories();
            iCatID = Convert.ToInt32(ddlCategory.Items[0].Value);
            GetArticleData();
        }
        iCatID = Convert.ToInt32(ddlCategory.SelectedValue);
        this.Page.ClientScript.RegisterOnSubmitStatement(
    this.GetType(),
    "AjaxHack",
    "for ( var i = 0; i < parent.frames.length; ++i ) if ( parent.frames[i].FCK ) parent.frames[i].FCK.UpdateLinkedField();"
    );
        //((Admin_MasterPage)Master).Centers= true;
    }

    private void GetCategories()
    {
        Dal.arParams.Clear();
        Dal.arParams.Add((int)NewsType.Diagnostic);
        ddlCategory.DataSource = Dal.ExecuteDBsp("usp_GetCategoryByParent", Dal.arParams);
        ddlCategory.DataBind();

        ddlCategory.SelectedIndex = 0;
    }
   
    protected void btnAddCategory_Click(Object sender ,EventArgs e)
    {
        string sTitle = txtTitle.Text;
        string sPageName = ab.ConvertToEnglishText(sTitle);
        string sqlDate = Helper.ConvertToSqlDateTime(DateTime.Now.ToString());
        int iArticle = Convert.ToInt32(hfArticleID.Value);
        Dal.arParams.Clear();
        Dal.arParams.Add(iArticle);
        Dal.arParams.Add(iCatID);
        Dal.arParams.Add(txtBrowserTitle.Text);
        Dal.arParams.Add(sTitle);
        Dal.arParams.Add(sPageName); //PageName
        Dal.arParams.Add(string.Empty);
        Dal.arParams.Add(edArticle.Value);
        Dal.arParams.Add(sqlDate.ToString());
        Dal.arParams.Add(true);
        Dal.arParams.Add(false);

        Dal.ExecuteDBsp("Admin_UpdateArticle", Dal.arParams, true);
    }

    
    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Load article
        txtTitle.Text = string.Empty;
        txtBrowserTitle.Text = string.Empty;
        edArticle.Value = string.Empty;
        GetArticleData();
    }

    private void GetArticleData()
    {
        
        article = Articles.GetArticleCatID(iCatID);

        if (article != null)
        {
            hfArticleID.Value = article.articleId.ToString();
            txtTitle.Text = article.articleName;
            txtBrowserTitle.Text = article.BrowserTitle;
            edArticle.Value = article.articleContext;
        }
        else
        {
            hfArticleID.Value = "0";
            txtTitle.Text = string.Empty;
            txtBrowserTitle.Text = string.Empty;
            edArticle.Value = string.Empty;
        }
    }
}
